﻿#include "UVideoPanel.h"
#include <qdebug.h>
#include "CVideoEnlargeDlg.h"

UVideoPanel::UVideoPanel(QWidget *parent)
	: UBasePanel(parent)
{
	ui.setupUi(this);
	m_bHwStart = false;
	m_bTxStart = false;
	m_bHwRecord = false;
	m_bTxIsRecord = false;
	//设置UI的窗口大小
	this->resize(1920 / 4 * 3, 1080 / 4 * 2);

	//设置按钮的标签显示
	ui.pushButtonVideoPlay->setToolTip(A2T("图像预览"));
	ui.pushButtonVideoRecord->setToolTip(A2T("图像录像"));
	ui.pushButtonVoideSnap->setToolTip(A2T("图像抓拍"));
	ui.pushButtonVideoEnlarge->setToolTip(A2T("图像放大"));
	ui.pushButtonInfraredPlay->setToolTip(A2T("红外预览"));
	ui.pushButtonInfraredRecord->setToolTip(A2T("红外录像"));
	ui.pushButtonInfraredSnap->setToolTip(A2T("红外抓拍"));
	ui.pushButtonInfraredEnlarge->setToolTip(A2T("红外放大"));
	ui.labelHwsp->setAttribute(Qt::WA_TransparentForMouseEvents, true);

	//初始化槽函数连接
	initConnect();
}

UVideoPanel::~UVideoPanel()
{
}

void UVideoPanel::setStyleSheetFile()
{
}

void UVideoPanel::initConnect()
{
	connect(ui.pushButtonVideoPlay, &QPushButton::clicked, this, &UVideoPanel::slotPushButtonVideoPlayClicked);
	connect(ui.pushButtonVideoRecord, &QPushButton::clicked, this, &UVideoPanel::slotPushButtonVideoRecordClicked);
	connect(ui.pushButtonVoideSnap, &QPushButton::clicked, this, &UVideoPanel::slotPushButtonVideoSnapClicked);
	connect(ui.pushButtonVideoEnlarge, &QPushButton::clicked, this, &UVideoPanel::slotPushButtonVideoEnlargeClicked);
	connect(ui.pushButtonInfraredPlay, &QPushButton::clicked, this, &UVideoPanel::slotPushButtonInfraredPlayClicked);
	connect(ui.pushButtonInfraredRecord, &QPushButton::clicked, this, &UVideoPanel::slotPushButtonInfraredRecordClicked);
	connect(ui.pushButtonInfraredSnap, &QPushButton::clicked, this, &UVideoPanel::slotPushButtonInfraredSnapClicked);
	connect(ui.pushButtonInfraredEnlarge, &QPushButton::clicked, this, &UVideoPanel::slotPushButtonInfraredEnlargeClicked);
	ui.pushButtonVideoRecord->hide();
	ui.pushButtonInfraredRecord->hide();
}

void UVideoPanel::slotStartPlay()
{

	{
		if (!m_bTxStart)
		{
			SCtrInfo info;
			info.m_hHwnd = (HWND)ui.labelTxsp->winId();
			info.m_pLabel = ui.labelTxsp;
			info.m_bCtrSync = false;
			info.m_nCtrCmd = enCRealPlay;
			info.m_nSubCtrDevType = enCamer;
			m_mapCtrId.insert("CAMER", "CAMER");
			info.m_sCtrId = "CAMER";
			bool bRet = m_pCamerCtrMange->ctrDevice(info);
			if (bRet == true)
			{
				ui.pushButtonVideoPlay->setIcon(QIcon(":/icon/Rob/StopPlay.png"));
				//ui.labelTxsp->setUpdatesEnabled(false);
				m_bTxStart = true;
			}
		}

	}
	//暂停一会儿
	QEventLoop loop;
	QTimer::singleShot(1000, &loop, SLOT(quit()));
	loop.exec();
	{
		if (!m_bHwStart)
		{
			SCtrInfo info;
			info.m_bCtrSync = false;
			info.m_hHwnd = (HWND)ui.labelHwsp->winId();
			info.m_pLabel = ui.labelHwsp;
			info.m_nCtrCmd = enCRealPlay;
			info.m_nSubCtrDevType = enIR;
			m_mapCtrId.insert("IR", "IR");
			info.m_sCtrId = "IR";
			bool bRet = m_pCamerCtrMange->ctrDevice(info);
			if (bRet == true)
			{
				ui.pushButtonInfraredPlay->setIcon(QIcon(":/icon/Rob/StopPlay.png"));
				//ui.labelHwsp->setUpdatesEnabled(false);
				m_bHwStart = true;
			}
		}
	}
	QTimer::singleShot(1000, &loop, SLOT(quit()));
	loop.exec();
}

void UVideoPanel::slotEndPlay()
{
	{
		SCtrInfo info;
		info.m_hHwnd = (HWND)ui.labelTxsp->winId();
		info.m_bCtrSync = false;
		info.m_nCtrCmd = enCStopRealPlay;
		info.m_nSubCtrDevType = enCamer;
		info.m_sCtrId = "CAMER";
		m_mapCtrId.insert("CAMER", "CAMER");
		bool bRet = m_pCamerCtrMange->ctrDevice(info);
		if (bRet == true)
		{
			ui.pushButtonVideoPlay->setIcon(QIcon(":/icon/Rob/play.png"));
			//ui.labelTxsp->setUpdatesEnabled(true);
			m_bTxStart = false;
		}
	}
	QEventLoop loop;
	QTimer::singleShot(1000, &loop, SLOT(quit()));
	loop.exec();
	{
		SCtrInfo info;
		info.m_bCtrSync = false;
		info.m_hHwnd = (HWND)ui.labelHwsp->winId();
		info.m_nCtrCmd = enCStopRealPlay;
		info.m_nSubCtrDevType = enIR;
		info.m_sCtrId = "IR";
		m_mapCtrId.insert("IR", "IR");
		bool bRet = m_pCamerCtrMange->ctrDevice(info);
		if (bRet == true)
		{
			ui.pushButtonInfraredPlay->setIcon(QIcon(":/icon/Rob/play.png"));
			//ui.labelHwsp->setUpdatesEnabled(true);
			m_bHwStart = false;
		}
	}
}
void UVideoPanel::slotPushButtonVideoPlayClicked()
{
	if (m_bTxStart == false)//开启预览
	{
		qDebug() << ui.labelTxsp->testAttribute(Qt::WA_NativeWindow);
		SCtrInfo info;
		info.m_hHwnd = (HWND)ui.labelTxsp->winId();
		info.m_pLabel = ui.labelTxsp;
		info.m_bCtrSync = false;
		info.m_nCtrCmd = enCRealPlay;
		info.m_nSubCtrDevType = enCamer;
		info.m_sCtrId = "CAMER";
		m_mapCtrId.insert("CAMER", "CAMER");
		bool bRet = m_pCamerCtrMange->ctrDevice(info);
		if (bRet == true)
		{
			ui.pushButtonVideoPlay->setIcon(QIcon(":/icon/Rob/StopPlay.png"));
			//ui.labelTxsp->setUpdatesEnabled(false);
			m_bTxStart = true;
		}
		qDebug() << ui.labelTxsp->testAttribute(Qt::WA_NativeWindow);
	}
	else//停止预览
	{
		SCtrInfo info;
		info.m_hHwnd = (HWND)ui.labelTxsp->winId();
		info.m_bCtrSync = false;
		info.m_nCtrCmd = enCStopRealPlay;
		info.m_nSubCtrDevType = enCamer;
		info.m_sCtrId = "CAMER";
		m_mapCtrId.insert("CAMER", "CAMER");
		bool bRet = m_pCamerCtrMange->ctrDevice(info);
		if (bRet == true)
		{
			ui.pushButtonVideoPlay->setIcon(QIcon(":/icon/Rob/play.png"));
			//ui.labelTxsp->setUpdatesEnabled(true);
			m_bTxStart = false;
		}
	}

}

void UVideoPanel::slotPushButtonVideoRecordClicked()
{
	if (m_bTxIsRecord == false)//开始图像录屏
	{
		if (m_bTxStart == false)
		{
			messageBox(0, A2T("请先开启预览后再进行录像"));
		}
		SCtrInfo info;
		info.m_nCtrCmd = enCRecord;
		info.m_nSubCtrDevType = enCamer;
		info.m_sVideoUrl = m_pDevData->m_pDevDataCam->m_sRecordPath + "Video/";
		info.m_hHwnd = (HWND)ui.labelTxsp->winId();
		bool bRet = m_pCamerCtrMange->ctrDevice(info);
		if (bRet == true)
		{
			ui.pushButtonVideoRecord->setIcon(QIcon(":/icon/Rob/StopRecord.png"));
			m_bTxIsRecord = true;
		}
	}
	else//结束图像录屏
	{
		SCtrInfo info;
		info.m_nCtrCmd = enCStopRecord;
		info.m_nSubCtrDevType = enCamer;
		info.m_sVideoUrl = m_pDevData->m_pDevDataCam->m_sRecordPath + "Video/";
		info.m_hHwnd = (HWND)ui.labelTxsp->winId();
		bool bRet = m_pCamerCtrMange->ctrDevice(info);
		if (bRet == true)
		{
			ui.pushButtonVideoRecord->setIcon(QIcon(":/icon/Rob/Record.png"));
			m_bTxIsRecord = false;
		}
	}
}

void UVideoPanel::slotPushButtonVideoSnapClicked()//图像抓拍
{
	SCtrInfo info;
	info.m_nCtrCmd = enCCapture;
	info.m_nSubCtrDevType = enCamer;
	info.m_sImgUrl = m_pDevData->m_pDevDataCam->m_sRecordPath + "Image/";
	info.m_bCtrSync = true;
	bool bRet=m_pCamerCtrMange->ctrDevice(info);
	if (bRet)
	{
		messageBox(0, A2T("抓拍成功,图片保存在:%1").arg(info.m_sImgUrl));
	}
}

void UVideoPanel::slotPushButtonVideoEnlargeClicked()//图像放大
{
	CVideoEnlargeDlg oper;
	oper.m_pCamerCtrMange = m_pCamerCtrMange;
	oper.m_nType = 0;
	oper.platVideo();
	oper.exec();
}

void UVideoPanel::slotPushButtonInfraredPlayClicked()
{
	if (m_bHwStart == false)//开始红外预览
	{
		SCtrInfo info;
		info.m_bCtrSync = false;
		info.m_hHwnd = (HWND)ui.labelHwsp->winId();
		info.m_pLabel = ui.labelHwsp;
		info.m_nCtrCmd = enCRealPlay;
		info.m_nSubCtrDevType = enIR;
		info.m_sCtrId = "IR";
		m_mapCtrId.insert("IR", "IR");
		bool bRet = m_pCamerCtrMange->ctrDevice(info);
		if (bRet == true)
		{
			ui.pushButtonInfraredPlay->setIcon(QIcon(":/icon/Rob/StopPlay.png"));
			//ui.labelHwsp->setUpdatesEnabled(false);
			m_bHwStart = true;
		}
	}
	else//停止红外预览
	{
		SCtrInfo info;
		info.m_bCtrSync = false;
		info.m_hHwnd = (HWND)ui.labelHwsp->winId();
		info.m_nCtrCmd = enCStopRealPlay;
		info.m_nSubCtrDevType = enIR;
		info.m_sCtrId = "IR";
		m_mapCtrId.insert("IR", "IR");
		bool bRet = m_pCamerCtrMange->ctrDevice(info);
		if (bRet == true)
		{
			ui.pushButtonInfraredPlay->setIcon(QIcon(":/icon/Rob/play.png"));
			//ui.labelHwsp->setUpdatesEnabled(true);
			m_bHwStart = false;
		}
	}
}

void UVideoPanel::slotPushButtonInfraredRecordClicked()
{
	if (m_bHwRecord == false)//红外开始录像
	{
		SCtrInfo info;
		info.m_nCtrCmd = enCRecord;
		info.m_nSubCtrDevType = enIR;
		info.m_hHwnd = (HWND)ui.labelHwsp->winId();
		info.m_sVideoUrl = m_pDevData->m_pDevDataCam->m_sRecordPath + "Video/";
		bool bRet = m_pCamerCtrMange->ctrDevice(info);
		if (bRet == true)
		{
			ui.pushButtonInfraredRecord->setIcon(QIcon(":/icon/Rob/StopRecord.png"));
			m_bHwRecord = true;
		}
	}
	else////红外结束录像
	{
		SCtrInfo info;
		info.m_nCtrCmd = enCStopRecord;
		info.m_nSubCtrDevType = enIR;
		info.m_sVideoUrl = m_pDevData->m_pDevDataCam->m_sRecordPath + "Video/";
		info.m_hHwnd = (HWND)ui.labelHwsp->winId();
		bool bRet = m_pCamerCtrMange->ctrDevice(info);
		if (bRet == true)
		{
			ui.pushButtonInfraredRecord->setIcon(QIcon(":/icon/Rob/Record.png"));
			m_bHwRecord = false;
		}
	}
}

void UVideoPanel::slotPushButtonInfraredSnapClicked()//红外抓拍
{
	SCtrInfo info;
	info.m_bCtrSync = true;
	info.m_nCtrCmd = enCCapture;
	info.m_nSubCtrDevType = enIR;
	info.m_sImgUrl = m_pDevData->m_pDevDataCam->m_sRecordPath + "Image/";
	info.m_bCtrSync = true;
	bool bRet = m_pCamerCtrMange->ctrDevice(info);
	if (bRet)
	{
		messageBox(0, A2T("抓拍成功,图片保存在:%1").arg(info.m_sImgUrl));
	}
}

void UVideoPanel::slotPushButtonInfraredEnlargeClicked()
{
	CVideoEnlargeDlg oper;
	oper.m_pCamerCtrMange = m_pCamerCtrMange;
	oper.m_nType = 1;
	oper.platVideo();
	oper.exec();
}

void UVideoPanel::slot_ExecResult(SCtrReData cCtrReData)
{
	if (!m_mapCtrId.contains(cCtrReData.sCtrId))
	{
		return;
	}
	//m_mapCtrId.remove(cCtrReData.sCtrId);
	if (!cCtrReData.nCtrType == enSubDev1)
	{
		return;
	}
	if (cCtrReData.nSubCtrDevType == enIR)
	{
		if (cCtrReData.nCtrType == enCRealPlay)
		{
			if (cCtrReData.nResult == 1)
			{
				ui.pushButtonInfraredPlay->setIcon(QIcon(":/icon/Rob/StopPlay.png"));
				//ui.labelHwsp->setUpdatesEnabled(false);
				m_bHwStart = true;
			}
		}

		if (cCtrReData.nCtrType == enCStopRealPlay)
		{
			if (cCtrReData.nResult == 1)
			{
				ui.pushButtonInfraredPlay->setIcon(QIcon(":/icon/Rob/play.png"));
				//ui.labelHwsp->setUpdatesEnabled(true);
				m_bHwStart = false;
			}
		}
	}
	else if (cCtrReData.nSubCtrDevType == enCamer)
	{
		if (cCtrReData.nCtrType == enCRealPlay)
		{
			if (cCtrReData.nResult == 1)
			{
				ui.pushButtonVideoPlay->setIcon(QIcon(":/icon/Rob/StopPlay.png"));
				//ui.labelTxsp->setUpdatesEnabled(false);
				m_bTxStart = true;
			}
		}

		if (cCtrReData.nCtrType == enCStopRealPlay)
		{
			if (cCtrReData.nResult == 1)
			{
				ui.pushButtonVideoPlay->setIcon(QIcon(":/icon/Rob/play.png"));
				//ui.labelTxsp->setUpdatesEnabled(true);
				m_bTxStart = false;
			}
		}
	}
}



void UVideoPanel::refreshData(bool bRefresh)
{
	if (bRefresh != m_bTxStart)
	{
		slotPushButtonVideoPlayClicked();
	}
	if (bRefresh != m_bHwStart)
	{
		slotPushButtonInfraredPlayClicked();
	}
}